<template>
  <div>
    <el-dialog
      :title="diaName"
      :visible.sync="addDiavisible"
      width="50%"
      :before-close="cancel"
      @opened="beforeopen"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="formRules"
        label-width="110px"
      >
        <el-form-item label="用户名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="account" v-if="diaType=='1'">
          <el-input
            v-model="form.account"
          ></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="account" v-if="diaType=='2'">
          <el-input
            v-model="form.account"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            auto-complete="new-password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPassword">
          <el-input v-model="form.checkPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="角色类型" prop="roleType" v-if="diaType=='1'">
          <el-select v-model="form.roleType" style="width: 100%">
            <el-option
              v-for="item in roleType"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addUser,getAllRole } from "@/api/family_doctor/family_doctor";
import { inputValidator, selectValidator } from "@/utils/validate";
export default {
  data() {
    var checkPassword = (rule, value, cb) => {
      if (value == this.form.password) {
        return cb();
      }
      cb("请确认两次密码是否相同");
    };
    return {
      addDiavisible: false,
      diaType: "",
      diaName: "",
      form: {
        account: undefined,
        password: undefined,
        checkPassword: undefined,
        name: undefined,
        roleType: undefined,
        roleId: undefined,
      },
      roleType: [
        {
          id: "3",
          name: "一级医生",
        },
        {
          id: "4",
          name: "二级医生",
        },
        {
          id: "5",
          name: "医院领导",
        },
      ],
      formRules: {
        name: inputValidator,
        account: inputValidator,
        password: inputValidator,
        checkPassword: [
          { required: true, trigger: "blur" },
          { validator: checkPassword, trigger: "blur" },
        ],
        roleType: selectValidator,
      },
    };
  },
  created() {},
  methods: {
    //关闭
    cancel() {
      for (var key in this.form) {
        this.form[key] = "";
      }
      this.addDiavisible = false;
      this.$emit("transData");
    },
    //弹窗渲染之前
    beforeopen() {
      if (this.diaType == "1") {
        this.diaName = "添加";
      } else {
        this.diaName = "修改";
      }
      this.$refs.formRef.clearValidate();
    },
    //提交
    addSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          addUser(this.form).then((res) => {
            if (res.code == "AA000000") {
              this.$message.success(this.diaName + "用户成功！");
            }
            this.cancel();
          });
        } else return false;
      });
    },
    //供父组件调用函数
    openDialog(data, row) {
		getAllRole().then(res=>{
		var roleList = 	res.data
			this.roleType = roleList
		})
      this.diaType = data;
      if (data == "2") {
        this.form = row;
      }
      this.addDiavisible = true;
    },
  },
};
</script>